package com.dengke.mybatis.test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import com.dengke.mybatis.entity.City;
import com.dengke.mybatis.mapper.CityMapper;

public class MybatisFisrt {

	@Test
	public void findCity() throws IOException {
		String resource = "SqlMapConfig.xml";

		InputStream inputStream = Resources.getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

		SqlSession sqlSession = sqlSessionFactory.openSession();
		
		CityMapper cityDao=sqlSession.getMapper(CityMapper.class);
		City city=cityDao.selectByPrimaryKey(1);
		
//		City city = sqlSession.selectOne("com.dengke.mybatis.mapper.getCity", 1);
		System.out.println(city.toString());
	}

	//@Test
//	public void getCityList() throws IOException {
//
//		String rescource = "SqlMapConfig.xml";
//		InputStream inputStream = Resources.getResourceAsStream(rescource);
//
//		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//
//		SqlSession sqlSession = sqlSessionFactory.openSession();
//		List<City> cities = sqlSession.selectList("com.dengke.mybatis.mapper.getCityList");
//		for (City city : cities) {
//			System.out.println(city.toString());
//		}
//	}
	
//	//@Test
//	public void insertCity() throws IOException{
//		String resource="SqlMapConfig.xml";
//		InputStream inputStream=Resources.getResourceAsStream(resource);
//		SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
//		SqlSession sqlSession=sqlSessionFactory.openSession();
//		City city=new City();
//		city.setName("ShangHai");
//		city.setCountryCode("CAN");
//		city.setDistrict("Asia");
//		city.setPopulation(250000);
//		sqlSession.insert("com.dengke.mybatis.mapper.insertCity", city);
//		//提交事务
//		sqlSession.commit();
//		System.out.println("City ID:"+city.getID());
//		sqlSession.close();
//	}
//	
//	//@Test
//	public void deleteCity() throws IOException{
//		String resource="SqlMapConfig.xml";
//		InputStream inputStream=Resources.getResourceAsStream(resource);
//		SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
//		SqlSession sqlSession=sqlSessionFactory.openSession();
//		sqlSession.delete("com.dengke.mybatis.mapper.deleteCity", 4082);
//		
//		sqlSession.commit();
//		sqlSession.close();
//	}
//	
//	@Test
//	public void selectCountryCode() throws IOException{
//		String resource="SqlMapConfig.xml";
//		InputStream inputStream=Resources.getResourceAsStream(resource);
//		SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
//		SqlSession sqlSession=sqlSessionFactory.openSession();
//		List<String>codeList= sqlSession.selectList("com.dengke.mybatis.mapper.selectCountryCode");
//		for (String code : codeList) {
//			System.out.println(code);
//		}
//	}
}
